package com.shenyue.nbtownlet.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.hitomi.glideloader.GlideImageLoader;
import com.hitomi.tilibrary.style.progress.ProgressPieIndicator;
import com.hitomi.tilibrary.transfer.TransferConfig;
import com.hitomi.tilibrary.transfer.Transferee;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.adapter.LabelListViewAdapter;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivityNoticeDetailBinding;
import com.shenyue.nbtownlet.databinding.BoxAttachmentItemBinding;
import com.shenyue.nbtownlet.databinding.ChoiceOfPersonnelBinding;
import com.shenyue.nbtownlet.databinding.SnapShotSheetDialogBinding;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.model.AppLoginModel;
import com.shenyue.nbtownlet.model.AppModel;
import com.shenyue.nbtownlet.model.AppNoticeModel;
import com.shenyue.nbtownlet.model.AppTaskModel;
import com.shenyue.nbtownlet.model.DefaultModel;
import com.shenyue.nbtownlet.model.FileUploadModel;
import com.shenyue.nbtownlet.util.DensityUtil;
import com.shenyue.nbtownlet.util.GlideUtil;
import com.shenyue.nbtownlet.util.StringUtil;
import com.trycatch.mysnackbar.Prompt;
import com.trycatch.mysnackbar.TSnackbar;

import org.sufficientlysecure.htmltextview.HtmlResImageGetter;

import java.util.ArrayList;
import java.util.List;

/**
 * 通知公告详情
 */

public class NoticeDetailActivity extends BaseActivity implements BaseListener, View.OnClickListener {
    private ActivityNoticeDetailBinding binding;
    private Context mContext;
    private Handler mHandler = new Handler();
    private JSONObject dataSource;
    private TSnackbar snackBar;
    private boolean isLoading;
    private JSONObject flowData;
    private JSONObject configData;
    private String action_type;
    private JSONArray flowSendData;

    private SnapShotSheetDialogBinding snapShotSheetDialogBinding;
    private final static int CHOICEPERSONNELAREA = 0x401;
    private List<ChoiceOfPersonnelBinding> choiceOfPersonnelList = new ArrayList<>();
    private ChoiceOfPersonnelBinding selectChoiceOfPersonnelBinding = null;
    private BottomSheetDialog sheetDialog;
    private boolean isView;
    private List<RadioButton> mRadioButtons = new ArrayList<>();

    private List<String> imageList = new ArrayList<>();
    private Transferee transferee;
    private List<ImageView> mList = new ArrayList<>();

    private int[] wh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_notice_detail);
        Bundle bundle = getIntent().getExtras();
        dataSource = JSON.parseObject(bundle.getString(DefaultModel.PAGE_DATA));
        String status = bundle.getString("status");
        isView = (!("0".equals(status) || StringUtil.isNull(status)));
        setActionBar(true, dataSource.getString("Title"));
        mContext = this;
        transferee = Transferee.getDefault(this);

        binding.btnSaveL.setVisibility(View.GONE);
        binding.btnSendL.setVisibility(View.GONE);
        binding.btnBackL.setVisibility(View.GONE);
        binding.btnCompleteL.setVisibility(View.GONE);
        binding.btnSave.setOnClickListener(this);
        binding.btnSend.setOnClickListener(this);
        binding.btnBack.setOnClickListener(this);
        binding.btnComplete.setOnClickListener(this);

        snapShotSheetDialogBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.snap_shot_sheet_dialog, null, false);
        snapShotSheetDialogBinding.btnCancel.setOnClickListener(new SendClickListener());
        snapShotSheetDialogBinding.btnSubmit.setOnClickListener(new SendClickListener());
        sheetDialog = new BottomSheetDialog(this);
        sheetDialog.setContentView(snapShotSheetDialogBinding.getRoot());
        sheetDialog.setCancelable(false);
        sheetDialog.setCanceledOnTouchOutside(false);
        BottomSheetBehavior mDialogBehavior = BottomSheetBehavior.from((View)snapShotSheetDialogBinding.getRoot().getParent());
        if(mDialogBehavior != null) {
            mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            mDialogBehavior.setPeekHeight(DensityUtil.dip2px(mContext, 1000));
        }

        wh = DensityUtil.getWH(this, 2, 24);

        snackBar = showTSnackbar("正在查询数据..., 请稍候");
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                findDetail();
            }
        }, 32);
    }

    private void findDetail() {
        JSONObject param = new JSONObject();
        param.put("id", StringUtil.dTs(dataSource.getString("InstanceID")));
        AppNoticeModel.GetNoticeData(mContext, param, this);
    }

    private void initView() {
        binding.CAPTION.setText(StringUtil.getValue(flowData.getString("CAPTION")));
        binding.CONTENT.setHtml(StringUtil.getValue(flowData.getString("CONTENT")),
                new HtmlResImageGetter(binding.CONTENT));
        String DRAFTTIME = StringUtil.getValue(flowData.getString("DRAFTTIME"));
        if (DRAFTTIME.contains("T"))
            DRAFTTIME = DRAFTTIME.substring(0, DRAFTTIME.indexOf("T"));
        binding.DRAFTTIME.setText(DRAFTTIME);
        binding.DRAFTUSER.setText(StringUtil.getValue(flowData.getString("DRAFTUSER")));
        //附件列表
        String ANNEXURL = StringUtil.getValue(flowData.getString("ANNEXURL"));
        /*ANNEXURL = "http://www.cnblogs.com/kissdodog/p/5419923.html" +
                ",https://pay.zyccst.com:8080/apk/1111.pdf" +
                ",https://pay.zyccst.com:8080/apk/1111.pptx" +
                ",https://pay.zyccst.com:8080/apk/1111.txt" +
                ",https://pay.zyccst.com:8080/apk/1111.xlsx" +
                ",http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg" +
                ",http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg" +
                ",http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg";*/
        if (StringUtil.isNull(ANNEXURL))
            return;
        binding.attr.setVisibility(View.VISIBLE);
        String[] files = ANNEXURL.split(",", -1);
        //图片
        int position = 0;
        for (int i = 0, j = files.length; i < j; i++) {
            String realpath = files[i];
            String type = realpath.substring(realpath.lastIndexOf(".") + 1);
            if (DefaultModel.PNG.equalsIgnoreCase(type) || DefaultModel.BMP.equalsIgnoreCase(type)
                    || DefaultModel.JPG.equalsIgnoreCase(type) || DefaultModel.JPEG.equalsIgnoreCase(type)) {
                imageList.add(realpath);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(wh[0], wh[1]);
                ImageView imageView = new ImageView(mContext);
                imageView.setLayoutParams(layoutParams);
                imageView.setId(position);
                imageView.setOnClickListener(this);
                binding.flexboxLayout.addView(imageView);
                mList.add(imageView);
                GlideUtil.loadImgCenterCrop(mContext, realpath, imageView);
                position++;
            }
        }
        //文件
        for (int i = 0, j = files.length; i < j; i++) {
            String realpath = files[i];
            String fileName = realpath.substring(realpath.lastIndexOf("/") + 1);
            String type = realpath.substring(realpath.lastIndexOf(".") + 1);
            if (DefaultModel.PPT.equalsIgnoreCase(type) || DefaultModel.PPTX.equalsIgnoreCase(type)
                    || DefaultModel.DOC.equalsIgnoreCase(type) || DefaultModel.DOCX.equalsIgnoreCase(type)
                    || DefaultModel.XLS.equalsIgnoreCase(type) || DefaultModel.XLSX.equalsIgnoreCase(type)
                    || DefaultModel.TXT.equalsIgnoreCase(type) || DefaultModel.PDF.equalsIgnoreCase(type)) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(wh[0], LinearLayout.LayoutParams.WRAP_CONTENT);
                View view = getAttachmentItem(type, fileName);
                view.setTag(type + "$_attr_$" + realpath);
                view.setLayoutParams(layoutParams);
                view.setOnClickListener(this);
                binding.flexboxLayout.addView(view);
            }
        }
    }

    private View getAttachmentItem(String type, String text) {
        BoxAttachmentItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.box_attachment_item, null, false);
        if (DefaultModel.DOC.equalsIgnoreCase(type) || DefaultModel.DOC.equalsIgnoreCase(type))
            binding.icon.setImageResource(R.mipmap.word);
        else if (DefaultModel.XLSX.equalsIgnoreCase(type) || DefaultModel.XLS.equalsIgnoreCase(type))
            binding.icon.setImageResource(R.mipmap.execl);
        else if (DefaultModel.PDF.equalsIgnoreCase(type))
            binding.icon.setImageResource(R.mipmap.pdf);
        else if (DefaultModel.PPT.equalsIgnoreCase(type) || DefaultModel.PPTX.equals(type))
            binding.icon.setImageResource(R.mipmap.ppt);
        else
            binding.icon.setImageResource(R.mipmap.txt);
        binding.name.setText(text);
        return binding.getRoot();
    }

    private void initConfig() {
        List<String> buttons = AppTaskModel.getButtons(mContext, configData);
        if (buttons.contains(AppTaskModel.BTN_SAVE))
            binding.btnSaveL.setVisibility(View.VISIBLE);
        if (buttons.contains(AppTaskModel.BTN_SEND))
            binding.btnSendL.setVisibility(View.VISIBLE);
        if (buttons.contains(AppTaskModel.BTN_BACK))
            binding.btnBackL.setVisibility(View.VISIBLE);
        if (buttons.contains(AppTaskModel.BTN_COMPLETED))
            binding.btnCompleteL.setVisibility(View.VISIBLE);
    }

    @Override
    public void success(String val, String action) {
        if (AppNoticeModel.GETNOTICEDATA_ACTION.equals(action)) {
            flowData = JSON.parseObject(val);
            initView();
            if (isView) {
                hideTSnackbar(snackBar, "数据查询成功");
                return;
            }
            JSONObject param = new JSONObject();
            param.put("userID", AppLoginModel.getIdentityId(mContext));
            param.put("flowid", dataSource.getString("FlowID"));
            param.put("formid", dataSource.getString("FormID"));
            param.put("instanceid", dataSource.getString("InstanceID"));
            param.put("stepid", dataSource.getString("StepID"));
            param.put("groupid", dataSource.getString("GroupID"));
            param.put("taskID", dataSource.getString("ID"));
            AppTaskModel.WaitListDetail(mContext, param, this);
        } else if (AppTaskModel.WAITLIST_DETAIL_ACTION.equals(action)) {
            hideTSnackbar(snackBar, "数据查询成功");
            configData = JSON.parseObject(val);
            initConfig();
        } else if (AppTaskModel.WAITLIST_EXECUTE_ACTION.equals(action)) {
            JSONObject data = JSON.parseObject(val);
            if ("true".equals(data.getString("IsSuccess"))) {
                hideTSnackbar(snackBar, data.getString("Messages"));
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("信息提交成功");
                builder.setMessage(data.getString("Messages"));
                builder.setCancelable(false);
                builder.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        setResult(RESULT_OK);
                        finish();
                    }
                });
                builder.show();
            } else {
                isLoading = false;
                hideTSnackbar(snackBar, data.getString("Message"), Prompt.WARNING);
            }
        } else if (AppTaskModel.WAITLIST_FLOWSEND_ACTION.equals(action) || AppTaskModel.WAITLIST_FLOWBACK_ACTION.equals(action)) {
            hideTSnackbar(snackBar, "请选择流程对应的人员");
            mRadioButtons.clear();
            flowSendData = JSON.parseArray(val);
            choiceOfPersonnelList.clear();
            snapShotSheetDialogBinding.flowSend.removeAllViews();
            for (int i = 0; i < flowSendData.size(); i++) {
                ChoiceOfPersonnelBinding root = getChoiceOfPersonnelBinding(flowSendData.getJSONObject(i));
                choiceOfPersonnelList.add(root);
                snapShotSheetDialogBinding.flowSend.addView(root.getRoot());
            }
            sheetDialog.show();
        } else if (AppTaskModel.WAITLIST_COMFIRMCOMPLETE_ACTION.equals(action)) {
            JSONArray array = JSON.parseArray(val);
            JSONObject data = array.getJSONObject(0);
            final String ctype = data.getString("ctype");
            if ("-1".equals(ctype)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("信息提交成功");
                builder.setMessage(data.getString("msg"));
                builder.setCancelable(false);
                builder.setPositiveButton("继续提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Execute(new JSONArray(), "", ctype);
                    }
                });
                builder.setNegativeButton("完成", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        setResult(RESULT_OK);
                        finish();
                    }
                });
                builder.show();
            } else {
                Execute(new JSONArray(), "", ctype);
            }
        }
    }

    @Override
    public void error(String error, String action) {
        isLoading = false;
        hideTSnackbar(snackBar, error, Prompt.WARNING);
    }

    @Override
    public void onClick(View v) {
        if (isLoading)
            return;
        switch (v.getId()) {
            case R.id.btn_send: {
                action_type = "submit";
                FlowSend();
                break;
            }
            case R.id.btn_save: {
                action_type = "save";
                onSave();
                break;
            }
            case R.id.btn_complete: {
                action_type = "Completed";
                onComplete();
                break;
            }
            case R.id.btn_back: {
                action_type = "back";
                onBack();
                break;
            }
            default:
                onView(v);
                break;
        }
    }

    private void onSave() {
        isLoading = true;
        snackBar = showTSnackbar("正在提交信息..., 请稍候");
        Execute(new JSONArray(), "", "");
    }

    private void onComplete() {
        isLoading = true;
        snackBar = showTSnackbar("正在提交信息..., 请稍候");
        JSONObject param = new JSONObject();
        param.put("userID", AppLoginModel.getIdentityId(mContext));
        param.put("flowid", dataSource.getString("FlowID"));
        param.put("formid", dataSource.getString("FormID"));
        param.put("instanceid", dataSource.getString("InstanceID"));
        param.put("stepid", dataSource.getString("StepID"));
        param.put("groupid", dataSource.getString("GroupID"));
        param.put("taskID", dataSource.getString("ID"));

        AppTaskModel.WaitListComfirmComplete(mContext, param, this);
    }

    private void onBack() {
        isLoading = true;
        snackBar = showTSnackbar("正在提交信息..., 请稍候");
        JSONObject param = new JSONObject();
        param.put("userID", AppLoginModel.getIdentityId(mContext));
        param.put("flowid", dataSource.getString("FlowID"));
        param.put("formid", dataSource.getString("FormID"));
        param.put("instanceid", dataSource.getString("InstanceID"));
        param.put("stepid", dataSource.getString("StepID"));
        param.put("groupid", dataSource.getString("GroupID"));
        param.put("taskID", dataSource.getString("ID"));

        AppTaskModel.WaitListFlowBack(mContext, param, this);
    }

    private void Execute(JSONArray steps, String comment, String ctype) {
        JSONObject param = new JSONObject();
        param.put("flowid", configData.getString("flowid"));
        param.put("instanceid", dataSource.getString("InstanceID"));
        param.put("stepid", configData.getString("stepid"));
        param.put("taskid", configData.getString("taskid"));
        param.put("userID", AppLoginModel.getIdentityId(mContext));
        param.put("Formid", dataSource.getString("FormID"));
        param.put("issign", StringUtil.isNull(configData.getString("issign")) ? "0" : configData.getString("issign"));
        param.put("comment", comment);
        param.put("taskname", dataSource.getString("Title"));
        if ("back".equals(action_type))
            param.put("groupid", dataSource.getString("GroupID"));
        else
            param.put("groupid", AppModel.GROUPID);
        param.put("ctype", ctype);
        JSONObject params = new JSONObject();
        params.put("type", action_type);
        params.put("steps", steps == null ? "" : steps);
        param.put("params1", params.toString());
        AppTaskModel.WaitListExecute(mContext, param, this);
    }

    private void FlowSend() {
        snackBar = showTSnackbar("正在提交信息..., 请稍候");
        JSONObject param = new JSONObject();
        param.put("areaid", AppLoginModel.getAreaId(mContext));
        param.put("instanceid", dataSource.getString("InstanceID"));
        param.put("stepid", configData.getString("stepid"));
        param.put("taskid", configData.getString("taskid"));
        param.put("userID", AppLoginModel.getIdentityId(mContext));
        param.put("tasktype", configData.getString("signType"));
        param.put("flowid", configData.getString("flowid"));
        param.put("groupid", AppModel.GROUPID);
        AppTaskModel.WaitListFlowSend(mContext, param, this);
    }

    private ChoiceOfPersonnelBinding getChoiceOfPersonnelBinding(final JSONObject data) {
        final ChoiceOfPersonnelBinding root = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.choice_of_personnel, null, false);
        root.radio.setText(StringUtil.isNull(data.getString("stepname")) ? data.getString("name") : data.getString("stepname"));
        root.radio.setChecked("1".equals(data.getString("ischeck")));
        if ("submit".equals(action_type)) {
            root.btnArea.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectChoiceOfPersonnelBinding = root;
                    Bundle bundle = new Bundle();
                    bundle.putString(DefaultModel.PAGE_DATA, "0");
                    bundle.putString("selectRang", data.getString("selectRang"));
                    bundle.putString("Isfilt", data.getString("isfilt"));
                    bundle.putString("STREETID", AppLoginModel.getAreaId(mContext));
                    toActivityForResult(ChoicePersonnelActivity.class, bundle, CHOICEPERSONNELAREA);
                }
            });
        } else
            root.btnArea.setVisibility(View.GONE);
        root.radio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < mRadioButtons.size(); i++) {
                    RadioButton rad = mRadioButtons.get(i);
                    if(rad == root.radio){
                        rad.setChecked(true);
                    }else{
                        rad.setChecked(false);
                    }
                }
            }
        });
        mRadioButtons.add(root.radio);
        LabelListViewAdapter listViewAdapter = new LabelListViewAdapter(mContext);
        root.personnelList.setAdapter(listViewAdapter);
        root.personnelList.setParentScrollView(snapShotSheetDialogBinding.scrollView);
        View view = root.getRoot();
        view.setTag(data.toString());
        return root;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null && requestCode == CHOICEPERSONNELAREA) {
            String val = data.getStringExtra(DefaultModel.PAGE_DATA);
            LabelListViewAdapter listViewAdapter = (LabelListViewAdapter) selectChoiceOfPersonnelBinding.personnelList.getAdapter();
            listViewAdapter.clear();
            listViewAdapter.addItems(JSON.parseArray(val));
        }
    }

    private class SendClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_cancel: {//取消
                    sheetDialog.hide();
                    isLoading = false;
                    break;
                }
                case R.id.btn_submit: {//提交
                    //步骤及人员信息字典数组
                    JSONArray array = new JSONArray();
                    for (int i = 0, j = flowSendData.size(); i < j; i++) {
                        final JSONObject flow = flowSendData.getJSONObject(i);
                        ChoiceOfPersonnelBinding root = choiceOfPersonnelList.get(i);
                        if(!root.radio.isChecked())
                            continue;
                        LabelListViewAdapter listViewAdapter = (LabelListViewAdapter) root.personnelList.getAdapter();
                        JSONArray items = listViewAdapter.getItems();
                        final StringBuilder sb = new StringBuilder();
                        for (int k = 0, h = items.size(); k < h; k++) {
                            JSONObject item = items.getJSONObject(k);
                            sb.append(item.getString("ID")).append(",");
                        }
                        if (sb.lastIndexOf(",") != -1)
                            sb.deleteCharAt(sb.lastIndexOf(","));
                        if(StringUtil.isNull(sb.toString()) && root.btnArea.getVisibility() == View.VISIBLE){
                            Toast.makeText(mContext, "请选择流程步骤对应的人员", Toast.LENGTH_LONG).show();
                            return;
                        }
                        array.add(new JSONObject() {{
                            put("id", StringUtil.isNull(flow.getString("stepid")) ? flow.getString("id") : flow.getString("stepid"));
                            put("member", StringUtil.isNull(sb.toString()) ? "" : sb.toString());
                        }});
                    }
                    if (array.isEmpty()) {
                        Toast.makeText(mContext, "请选择流程步骤", Toast.LENGTH_LONG).show();
                        return;
                    }
                    sheetDialog.hide();
                    snackBar = showTSnackbar("正在提交数据..., 请稍候");
                    Execute(array, snapShotSheetDialogBinding.comment.getText().toString().trim(), "");
                    break;
                }
            }
        }
    }

    private void doCancel() {
        String msg = "数据尚未提交，您确定要退出吗？";
        if (isLoading)
            msg = "数据正在提交，您确定要退出吗？";
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("温馨提示")
                .setMessage(msg)
                .setPositiveButton("点错了", null)
                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        FileUploadModel.stop();
                        dialogInterface.dismiss();
                        finish();
                    }
                });
        builder.create().show();
    }

    @Override
    public void onBackPressed() {
        if (!isView) {
            doCancel();
        } else
            super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (transferee != null)
            transferee.destroy();
    }

    private void onView(View view){
        if (view instanceof ImageView) {
            previewPhoto(view.getId());
            return;
        }
        String tag = (String) view.getTag();
        String[] s = tag.split("\\$_attr_\\$");
        String type = s[0];
        String url = s[1];
        if (DefaultModel.PDF.equalsIgnoreCase(type)) {
            Bundle bundle = new Bundle();
            bundle.putString(DefaultModel.URL, url);
            toActivity(PDFActivity.class, bundle);
        } else if (DefaultModel.DOC.equalsIgnoreCase(type) || DefaultModel.DOCX.equalsIgnoreCase(type)) {
            Bundle bundle = new Bundle();
            bundle.putString(DefaultModel.URL, url);
            toActivity(OfficeActivity.class, bundle);
        } else if (DefaultModel.XLS.equalsIgnoreCase(type) || DefaultModel.XLSX.equalsIgnoreCase(type)) {
            Bundle bundle = new Bundle();
            bundle.putString(DefaultModel.URL, url);
            toActivity(OfficeActivity.class, bundle);
        } else if (DefaultModel.PPT.equalsIgnoreCase(type) || DefaultModel.PPTX.equalsIgnoreCase(type)) {
            Bundle bundle = new Bundle();
            bundle.putString(DefaultModel.URL, url);
            toActivity(OfficeActivity.class, bundle);
        } else if (DefaultModel.TXT.equalsIgnoreCase(type)) {
            Bundle bundle = new Bundle();
            bundle.putString(DefaultModel.URL, url);
            toActivity(TXTActivity.class, bundle);
        }
    }

    private void previewPhoto(int index) {
        TransferConfig config = TransferConfig.build()
                .setNowThumbnailIndex(index)
                .setSourceImageList(imageList)
                .setMissPlaceHolder(R.mipmap.ic_empty_photo)
                .setOriginImageList(mList)
                .setProgressIndicator(new ProgressPieIndicator())
                .setImageLoader(GlideImageLoader.with(getApplicationContext()))
                .create();
        transferee.apply(config).show(new Transferee.OnTransfereeStateChangeListener() {
            @Override
            public void onShow() {
                Glide.with(mContext).pauseRequests();
            }

            @Override
            public void onDismiss() {
                Glide.with(mContext).resumeRequests();
            }
        });
    }
}
